<?php

namespace V6;
class  C_CaptchaAccount extends \ResourceClass
{

    public $isActive = true;
    public $remark = '验证客户信息';

    public function get(array $req, array $post, &$result)
    {
        try {
            if (!isset($req['account'])) {
                return 'require account false';
            }
            if (!isset($req['captcha_code'])) {
                return 'require captcha_code false';
            }
            if (!isset($req['regid'])) {
                return 'require regid false';
            }
            if (!isset($req['token'])) {
                return 'require  token false';
            }
            $token = md5($req['regid'].'XIANQIAO520');  //生成令牌token
            if($token != $req['token']){                //检测token
                return 'require token false';
            }
            //连接memcache
            $memcache = new \Memcache();
            $con = $memcache->connect(\Util\Config::get('memcache_host', 'Memcache'), \Util\Config::get('memcache_port', 'Memcache'));
            if (!$con) {
                return 0;
            }
            //获取 储存信息
            $captcha_code = $memcache->get('CAPTCHA_CODE_'.$req['regid']);
            if($captcha_code == false){
                return '图片识别码失效';
            }
            $account = preg_replace('/[(\xc2\xa0)|\s]+/','', $req['account']);
            $captcha_code_pic = preg_replace('/[(\xc2\xa0)|\s]+/','', $req['captcha_code']);
            if($captcha_code_pic == $captcha_code){      //判断图片识别码
                $memcache->delete('CAPTCHA_CODE_'.$req['regid']);
                $user_handler = new \Biz\UserHandler();
                $check_result = $user_handler->checkAccount($account);   //检测account
                if($check_result){
                    $user_id = $check_result->getId();
                    $code = $num = rand(1000, 9999); //随机数
                    $push_message_handler = new \Biz\PushMessageHandler();
                    $push_message_cls = new \Cls\PushMessage\PushMessage();
                    $content = '您正在使用手机号找回密码，验证码为：【'.$code.'】，请在10分钟内完成验证。';
                    $push_message_cls->create($user_id,$check_result->getTelephone(),$content);
                    //$push_message_cls->create($user_id,'13773668430',$code);
                    $push_message_cls->setTitle('手机验证码：');
                    $push_message_cls->setType(\Cls\PushMessage\PushMessage::MESSAGE_TYPE_SMS);
                    $result_insert = $push_message_handler->insert($push_message_cls);  //发送短信验证码
                    if($result_insert){
                        $memcache->set('MOBILE_VALIDATE_CODE_'.$user_id,$code,MEMCACHE_COMPRESSED,345600); //将验证码缓存
                        if($check_result->getTelephone() != false){
                            $tmp['mobile'] = substr_replace($check_result->getTelephone(), '****', 3, 4);
                        }else{
                            $tmp['mobile'] = '';
                        }
                        $tmp['uid'] = $user_id;
                        $result = $tmp;
                    }
                }else{
                    return '未知登录账号';
                }
            }else{
                return '图片识别码失效';
            }
        } catch (\Exception $e) {
            return $e->getMessage();
        }

    }
}